package mr.zhang.dao.impl;

import mr.zhang.dao.CustomerDao;
import mr.zhang.pojo.model.TCustomer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jerry on 15/2/2.
 * E-mail:huaxinxc@me.com
 */
@Repository("customerDao")
public class CustomerDaoImpl  implements CustomerDao {
    Logger logger = LoggerFactory.getLogger(CustomerDaoImpl.class);
    @Autowired

    SessionFactory sessionFactory ;
    @Override
    public  List<TCustomer> queryCustomerList() {
        List<TCustomer> customerList = new ArrayList<TCustomer>();

       Session session = sessionFactory.openSession();

        String sql="from TCustomer";

        customerList = session.createQuery(sql).list();



        session.close();
        return customerList;
    }

    /**
     * 添加客户
     *
     * @param customer
     */
    @Override
    @Qualifier
    public void addCustomer(TCustomer customer) {
        Session session = sessionFactory.openSession();
        Transaction tx =  session.beginTransaction();

        session.save(customer);
        tx.commit();
        session.close();


    }
}
